/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.view;

import java.io.File;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import client.domainModels.NodeImpl.Actor;
import client.domainModels.NodeImpl.Case;
import client.domainModels.NodeImpl.Conversation;
import client.domainModels.NodeImpl.Event;
import client.domainModels.NodeImpl.LogFile;
import client.domainModels.NodeImpl.Performatif;
import client.domainModels.NodeImpl.TimeStamp;
import client.domainModels.NodeImpl.Receiver;
import client.domainModels.NodeImpl.Receivers;
import client.domainModels.NodeImpl.Task;

/**
 * 
 * @author HAO Jianguo
 */
public class LogToMemory {

	public static LogFile logToMemoryAndDisplay(File file) {	
		int eventIDGen = 0;
		LogFile log = new LogFile();
		try {
			XMLWriter writer = null;
			SAXReader reader = new SAXReader();
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("GBK");

			if (file.exists()) {
				Document document = reader.read(file);
				Element root = document.getRootElement();
				
				for (Iterator i = root.elementIterator("case"); i.hasNext();) {
					// Tag case
					Element _case = (Element) i.next();
					// Case name
					String caseName = _case.attributeValue("name");
					// Object Case
					Case object_case = new Case();
					object_case.setID(caseName);
					// Case Element - event
					for (Iterator j = _case.elementIterator("event"); j
							.hasNext();) {
						// Tag event
						Element event = (Element) j.next();
						// *Object Event
						Event object_event = new Event();
						++eventIDGen;
						object_event.setID("E" + eventIDGen);
						// Tag Task
						Task task = new Task();
						String taskName = event.element("task").attributeValue(
								"name");
						task.setID(taskName);
						// Tag Performative
						Performatif performative = new Performatif();
						String performatifName = event.element("performatif")
								.attributeValue("name");
						performative.setID(performatifName);
						// Tag Actor
						Actor actor = new Actor();
						String actorName = event.element("actor")
								.attributeValue("name");
						actor.setID(actorName);
						// Tag Conversation
						Conversation conversation = new Conversation();
						String conversationID = event.element("conversation")
								.attributeValue("id");
						conversation.setID(conversationID);
						// Tag Date
						TimeStamp date = new TimeStamp();
						String dateInString = event.element("timestamp")
								.attributeValue("date");
						date.setID(dateInString);

						// date.
						ArrayList<String> listReceviers = new ArrayList<String>();
						Receivers receivers = new Receivers();
						for (Iterator k = event.elementIterator("receivers"); k
								.hasNext();) {
							Element recs = (Element) k.next();
							for (Iterator l = recs.elementIterator("receiver"); l
									.hasNext();) {
								Element rec = (Element) l.next();
								String receiverName = rec
										.attributeValue("name");
								Receiver receiver = new Receiver();
								receiver.setID(receiverName);
								receivers.addChildNode(receiver);
							}
						}
						object_event.addChildNode(task);
						object_event.addChildNode(performative);
						object_event.addChildNode(actor);
						object_event.addChildNode(receivers);
						object_event.addChildNode(conversation);
						object_event.addChildNode(date);
						object_case.addChildNode(object_event);
					}
					log.addChildNode(object_case);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return log;
	}

	
}
